<template>
  <div class="common-layout">
    <el-container>
      <el-header style="display: flex;align-items: center;">
        <h2>权限管理系统</h2>
        <div style="width: 130px;position: absolute;right: 0;">
          <el-dropdown>
            <div style="cursor: pointer;align-items: center;display: inline-flex;margin-left: -30px">
              <el-avatar icon="UserFilled" />
              <p v-if="user.name != null" style="margin-left: 10px">{{user.name}}</p>

            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-aside style="width: 100%;">
        <el-menu
            router
            :default-active="$route.path"
            mode="horizontal"
            class="el-menu-demo"
            background-color="#545c64"
            text-color="#fff"
        >
          <!--单级菜单-->
          <el-menu-item v-show="p1.isOne === 1" :index="p1.visitPath" v-for="p1 in user.permList" :key="p1.id">
            <img v-if="p1.icon != null" :src="require('@/assets/imgs/menuIcon/' + p1.icon)" class="imgSideIcon" alt="">
            {{ p1.name }}
          </el-menu-item>
          <!--二级菜单-->
          <el-sub-menu :index="p2.path" v-show="p2.isOne === 0" v-for="p2 in user.permList">
            <template #title>
              <img v-if="p2.icon != null" :src="require('@/assets/imgs/menuIcon/' + p2.icon)" class="imgSideIcon" alt="">
              {{ p2.name }}
            </template>
            <el-menu-item :index="p3.visitPath" :key="p3.id" v-for="p3 in p2.perms">
              <img v-if="p3.icon != null" :src="require('@/assets/imgs/menuIcon/' + p3.icon)" class="imgSideIcon" alt="">
              {{ p3.name}}
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-main style="height: 83.8vh;">
        <router-view @update:user="updateUser" />
      </el-main>
    </el-container>
  </div>
</template>

<script lang="js" setup>
import {ref, reactive, getCurrentInstance} from "vue";
import {UserFilled} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";


const {proxy} = getCurrentInstance();

let user = reactive(JSON.parse(sessionStorage.getItem('user-data')));



let logout = () =>{
  sessionStorage.removeItem('user-data');
  proxy.$router.push('/login');
  ElMessage({message:'退出成功！',type:'success'})
}

//接受子级更新的数据
const updateUser = (zUser) => {
  user = JSON.parse(JSON.stringify(zUser)); // 进行深拷贝
};

</script>
<style scoped>

</style>